package com.hgicreate.app.md2docx;

import com.hgicreate.app.md2docx.extension.kroki.internal.KrokiNodeDocxRender;
import com.vladsch.flexmark.docx.converter.DocxRenderer;
import com.vladsch.flexmark.ext.definition.DefinitionExtension;
import com.vladsch.flexmark.ext.emoji.EmojiExtension;
import com.vladsch.flexmark.ext.footnotes.FootnoteExtension;
import com.vladsch.flexmark.ext.gfm.strikethrough.StrikethroughSubscriptExtension;
import com.vladsch.flexmark.ext.gitlab.GitLabExtension;
import com.vladsch.flexmark.ext.ins.InsExtension;
import com.vladsch.flexmark.ext.superscript.SuperscriptExtension;
import com.vladsch.flexmark.ext.tables.TablesExtension;
import com.vladsch.flexmark.ext.toc.SimTocExtension;
import com.vladsch.flexmark.ext.toc.TocExtension;
import com.vladsch.flexmark.ext.wikilink.WikiLinkExtension;
import com.vladsch.flexmark.parser.Parser;
import com.vladsch.flexmark.util.ast.Node;
import com.vladsch.flexmark.util.data.DataHolder;
import com.vladsch.flexmark.util.data.MutableDataSet;
import org.docx4j.Docx4J;
import org.docx4j.openpackaging.exceptions.Docx4JException;
import org.docx4j.openpackaging.packages.WordprocessingMLPackage;

import java.io.File;
import java.util.Arrays;


public class FlexMarkTest {
    // don't need to use pegdown options adapter. You can setup the options as you like. I find this is a quick way to add all the fixings
    final private static DataHolder OPTIONS = new MutableDataSet()
            .set(Parser.EXTENSIONS, Arrays.asList(
                    DefinitionExtension.create(),
                    GitLabExtension.create(),
                    EmojiExtension.create(),
                    FootnoteExtension.create(),
                    StrikethroughSubscriptExtension.create(),
                    InsExtension.create(),
                    SuperscriptExtension.create(),
                    TablesExtension.create(),
                    TocExtension.create(),
                    SimTocExtension.create(),
                    WikiLinkExtension.create()
            ))
            .set(DocxRenderer.SUPPRESS_HTML, true)
            // the following two are needed to allow doc relative and site relative address resolution
            .set(DocxRenderer.DOC_RELATIVE_URL, "file:///Users/vlad/src/pdf") // this will be used for URLs like 'images/...' or './' or '../'
            .set(DocxRenderer.DOC_ROOT_URL, "file:///Users/vlad/src/pdf") // this will be used for URLs like: '/...'
            .set(GitLabExtension.RENDER_BLOCK_MERMAID, true) // this will be used for URLs like: '/...'
            ;


    public static void main(String[] args) {
        String markdown = """
                ## 二级标题
                
                以下为公司质量管理体系认证关键数据概览： \s
                
                | 认证要素                | 实施情况                                                                 |
                |-------------------------|--------------------------------------------------------------------------|
                | 认证标准                | ISO 9001:2015（证书编号：CNAS-QM-2023-XXXX）                            |
                | 覆盖范围                | 无线通信设备研发、无人机系统集成、7×24小时运维服务                       |
                | 最近监督审核时间        | 2024年3月（无不符合项）                                                 |
                | 特殊过程控制            | 无人机飞行测试实行双岗互检制度，关键参数100%自动化记录                   |
                | 持续改进成果            | 近三年客户满意度提升8.2%，故障率同比下降37%                              |
                
                ```mermaid
                graph TD
                    A[认证申请] --> B[合同评审]
                    B --> C{是否受理?}
                    C -->|是| D[第一阶段审核]
                    C -->|否| E[补充材料]
                    D --> F[文件评审]
                    F --> G[现场审核]
                    G --> H{发现不符合项?}
                    H -->|无| I[颁发证书]
                    H -->|有| J[整改验证]
                    J --> K[认证决定]
                ```
                """;

        Parser PARSER = Parser.builder(OPTIONS).build();
        DocxRenderer RENDERER = DocxRenderer.builder(OPTIONS)
                .nodeFormatterFactory(new KrokiNodeDocxRender.Factory())
                .build();

        Node document = PARSER.parse(markdown);

        // to get XML
        String xml = RENDERER.render(document);

        // or to control the package
        WordprocessingMLPackage template = DocxRenderer.getDefaultTemplate();
        RENDERER.render(document, template);

        File file = new File("./flexmark-java-issue-176.docx");
        try {
            template.save(file, Docx4J.FLAG_SAVE_ZIP_FILE);
        } catch (Docx4JException e) {
            e.printStackTrace();
        }
    }
}
